package inutan.game.action;

import inutan.common.constant.DataConstant;
import inutan.game.business.GameFlowContext;
import inutan.game.business.ItemTypeManager;
import inutan.game.entity.prop.BaseItemInfo;
import inutan.game.prop.OperatePropInterface;
import inutan.game.proto.ItemProto;
import luban.common.Command;
import org.ricks.ioc.anno.Action;
import org.ricks.ioc.anno.ActionMethod;
import org.ricks.net.DataCodecKit;
import org.ricks.net.DefaultPacket;
import java.util.ArrayList;
import java.util.List;

@Action(modularId = 1)
public class PropController {


    @ActionMethod(cmd = Command.GetItems)
    public void GetItems(GameFlowContext<DefaultPacket> context){
        DefaultPacket defaultPacket = context.getPacket();
        Integer itemType = DataCodecKit.decode(defaultPacket.getPacket(),Integer.class);
        OperatePropInterface operatePropInterface = ItemTypeManager.getOperatePropTask(itemType);
        List<BaseItemInfo> BaseItemInfoList = operatePropInterface.props(context);
        if(BaseItemInfoList.size()>1000){
            ListByIndex(BaseItemInfoList,1000).forEach(x->{
                context.sendResponse(DataConstant.BROADCAST_MESSAGE,Command.GetItems, ItemProto.resItemInfo(itemType, x));
            });
        }else context.sendResponse(DataConstant.BROADCAST_MESSAGE,Command.GetItems, ItemProto.resItemInfo(itemType, BaseItemInfoList));
    }

    private List<List<BaseItemInfo>> ListByIndex(List<BaseItemInfo> list, int number){
        int index = 0;
        List<List<BaseItemInfo>> outList = new ArrayList<>();
        for (int i = 0; i <= (list.size() / number); i++) {
            List<BaseItemInfo> out = new ArrayList<>();
            int endindex = index + number > list.size() ? list.size() : index + number;
            for (int n = index; n < endindex; n++) {
                out.add(list.get(n));
            }
            outList.add(out);
            index += number;
        }
        return outList;
    }
}
